package com.siriframe.security.controller;


import com.siriframe.entity.Result;
import com.siriframe.security.dao.UserMapper;
import com.siriframe.security.entity.dto.UserRegisterDTO;
import com.siriframe.security.service.impl.LoginAndLogoutServiceImpl;
import com.siriframe.security.service.impl.UserManagementServiceImpl;
import com.siriframe.security.service.impl.UserRegisterServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Api(tags = "用户登录注册接口")
@RestController
public class SecurityController {
    @Autowired
    private LoginAndLogoutServiceImpl loginAndLogoutService;

    @Autowired
    private UserRegisterServiceImpl userRegisterService;

    @Autowired
    private UserManagementServiceImpl userManagementService;

    @Autowired
    private UserMapper userMapper;

    @ApiOperation("用户登录")
    @PostMapping("/login")
    public Result login(@RequestParam("username") String username, @RequestParam("password") String password,
                        HttpServletResponse response){
        response.setHeader("Access-Control-Allow-Origin","*");
        Result result = loginAndLogoutService.login(username, password);
        return result;
    }

    @ApiOperation("用户退出登录")
    @DeleteMapping("/exit")
    public Result logout(HttpServletResponse response){
        response.setHeader("Access-Control-Allow-Origin","*");
        Result result = loginAndLogoutService.logout();
        return result;
    }

    @ApiOperation("用户注册")
    @PostMapping("/register")
    public Result register(@RequestBody UserRegisterDTO userRegisterDTO, HttpServletResponse response){

        response.setHeader("Access-Control-Allow-Origin","*");
        Result result = userRegisterService.userRegister(userRegisterDTO);
        return result;
    }

    @ApiOperation("管理员注册")
    @PostMapping("/registerSystemUser")
    public Result registerSystemUser(@RequestBody UserRegisterDTO userRegisterDTO,HttpServletResponse response){

        response.setHeader("Access-Control-Allow-Origin","*");
        Result result = userRegisterService.userSystemRegister(userRegisterDTO);
        return result;
    }

    @ApiOperation("用户角色权限修改")
    @PutMapping("/updatePermission")
    public Result updatePermission(HttpServletResponse response,@RequestParam("userId") Long userId,@RequestParam("role") String role){
        response.setHeader("Access-Control-Allow-Origin","*");
        Result result = userManagementService.updatePermission(userId, role);
        return result;
    }

    @GetMapping("/getUserInformationByToken")
    public Result getUserInformationByToken(HttpServletResponse response, HttpServletRequest request){

        response.setHeader("Access-Control-Allow-Origin","*");

        Result userInformation = userManagementService.getUserInformationByToken(request);
        return userInformation;
    }

    @GetMapping("/getUserInformationByUserId")
    public Result getUserInformationByUserId(HttpServletResponse response,@RequestParam("userId") Long userId){

        response.setHeader("Access-Control-Allow-Origin","*");

        Result userInformation = userManagementService.getUserInformationByUserId(userId);
        return userInformation;
    }
}



